package com.xidian.zhiancloud.controller.dangerWorkTicket.workManager.WorkTicket;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.qo.dangerWorkTicket.TemplateListQO;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketQO;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketTemplateService;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketDTOVO;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 第二页，危险作业申请的下半部分，完整地创建新的作业票。操作包括添加、删除、查看、修改（编辑）、列表查找。使用t_template，更名为t_work_ticket
 *
 * 延期申请中，MyWorkTicket.java的isDelay字段默认为null或false，表示默认不延期；当点击延期时（PatchById），将该字段改为true。
 * 新建一个小表t_work_ticket（更名为t_delay_apply），连接大表id并填充“延期时间”和“延期原因”两个字段。
 */

@RequestMapping("WorkTicket")
@RestController
public class WorkTicketController extends BaseController {
    @Autowired
    private WorkTicketService workTicketService;

    @Autowired
    private WorkTicketTemplateService workTicketTemplateService;

    /**
     * 新增作业票。
     *
     * @param workTicket
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(WorkTicket workTicket) {
        workTicket.setCompanyId(getSessionUserCompanyId());
        workTicket.setCreatorId(getSessionUserid());
        workTicket.setCreateTime(new Date(System.currentTimeMillis()));
        // 新建作业票，为待提交状态
        workTicket.setWorkStatus(0);
        Boolean res = workTicketService.save(workTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    @PostMapping("/commit")
    public ResultDTO<Boolean> commitTicket(@RequestParam("id") Long id) {
        WorkTicket workTicket = workTicketService.getById(id);
        if (workTicket == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        // 新建作业票，为审批中
        workTicket.setWorkStatus(1);
        Boolean res = workTicketService.updateById(workTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 新增作业票第一步，向前端返回Json字段。使用WorkTicketTemplate.getList()方法向每个CompanyId发送Status=1的数据。
     *
     * @param templateId
     * @return 是否成功返回Json字段
     */
    @GetMapping("/returnTicket")
    public ResultDTO<List<String>> returnTicket(@RequestParam("templateId") Long templateId) {
        int isOpen = workTicketTemplateService.getById(templateId).getStatus();
        if (isOpen == 0) {
            System.out.println("该模板已停用");
            return ResultDTO.successOf(null, GeneralResponseEnums.REQUEST_ERROR);
        }
        List<String> res = workTicketTemplateService.riskList(templateId);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据公司id向前端提供作业票列表：Map<作业票模板name,作业票模板id>。
     *
     * @param
     * @return
     */
    @GetMapping("/templateListMap")
    public ResultDTO<LinkedHashMap> templateListMap() {
        TemplateListQO templateListQO = new TemplateListQO();
        templateListQO.setCompanyId(getSessionUserCompanyId());
        templateListQO.setStatus(1);
        LinkedHashMap<Long, String> templateListMap = workTicketTemplateService.getListByQO(templateListQO);
        return ResultDTO.successOf(templateListMap, GeneralResponseEnums.SUCCESS);
    }


    /**
     * 根据id删除作业票。
     *
     * @param ids
     * @return 是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean res = workTicketService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }


    /**
     * 根据id查询作业票
     *
     * @param id
     * @return 作业票
     */
    @GetMapping("/getById")
    public ResultDTO<WorkTicketDTOVO> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(workTicketService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 根据id修改作业票
     *
     * @param workTicket
     * @return 是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(WorkTicket workTicket)  {
        workTicket.setCompanyId(null);
        workTicket.setCreatorId(null);
        workTicket.setCreateTime(null);
        Boolean res = workTicketService.updateById(workTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 条件列表查询
     *
     * @param workTicketQO
     * @return 作业票列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<WorkTicketVO>> getListByQO(WorkTicketQO workTicketQO) {
        if(workTicketQO.getCurrent() == null || workTicketQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicketQO.setCreatorId(getSessionUserid());
        workTicketQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(workTicketService.getListByQO(workTicketQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 延期申请
     *
     * @param id
     * @param delayTime
     * @param delayReason
     * @return 是否成功申请延期
     */
    @PostMapping("/delayById")
    public ResultDTO<Boolean> delayById(@RequestParam("id") Long id,
                                        @RequestParam("delayTime") Date delayTime,
                                        @RequestParam("delayReason") Integer delayReason) {
        WorkTicket workTicket = workTicketService.getById(id);
        workTicket.setIsDelay(1);
        workTicket.setDelayTime(delayTime);
        workTicket.setDelayReason(delayReason);
        workTicket.setDelayApplicantId(getSessionUserid());
        boolean res = workTicketService.updateById(workTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }
}





